{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "85a58361",
   "metadata": {},
   "source": [
    "#### 写出导入pandas的语句，并且给pandas别名为`pd`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4c69b47e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "775988b2",
   "metadata": {},
   "source": [
    "#### 创建一个叫`name`的Series，里面包含字符串元素 `\"小陈\", \"小李\", \"小王\", \"小张\", \"小赵\", \"小周\"`，标签为`\"001\", \"002\", \"003\", \"004\", \"005\", \"006\"`。\n",
    "#### 创建一个叫`gender`的Series，里面包含字符串元素 `\"女\", \"女\", \"男\", \"男\", \"女\", \"男\"`，标签为`\"006\", \"005\", \"004\", \"003\", \"002\", \"001\"`。\n",
    "#### 创建一个叫`height`的Series，里面包含浮点数元素 `172.5, 168.0, 178.2, 181.3, 161.7`，标签为`\"001\", \"002\", \"003\", \"004\", \"005\"`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "439581d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = pd.Series([\"小陈\", \"小李\", \"小王\", \"小张\", \"小赵\", \"小周\"], index=[\"001\", \"002\", \"003\", \"004\", \"005\", \"006\"])\n",
    "gender = pd.Series([\"女\", \"女\", \"男\", \"男\", \"女\", \"男\"], index=[\"006\", \"005\", \"004\", \"003\", \"002\", \"001\"])\n",
    "height = pd.Series([172.5, 168.0, 178.2, 181.3, 161.7], index=[\"001\", \"002\", \"003\", \"004\", \"005\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2762c535",
   "metadata": {},
   "source": [
    "#### 创建一个叫`students`的DataFrame，包含列名姓名、性别、身高，数据分别对应创建好的`name`、`gender`、以及`height`这三个Series。输出创建好的`students`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6f0b4181",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>001</th>\n",
       "      <td>小陈</td>\n",
       "      <td>男</td>\n",
       "      <td>172.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002</th>\n",
       "      <td>小李</td>\n",
       "      <td>女</td>\n",
       "      <td>168.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>003</th>\n",
       "      <td>小王</td>\n",
       "      <td>男</td>\n",
       "      <td>178.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>004</th>\n",
       "      <td>小张</td>\n",
       "      <td>男</td>\n",
       "      <td>181.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>005</th>\n",
       "      <td>小赵</td>\n",
       "      <td>女</td>\n",
       "      <td>161.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>006</th>\n",
       "      <td>小周</td>\n",
       "      <td>女</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     姓名 性别     身高\n",
       "001  小陈  男  172.5\n",
       "002  小李  女  168.0\n",
       "003  小王  男  178.2\n",
       "004  小张  男  181.3\n",
       "005  小赵  女  161.7\n",
       "006  小周  女    NaN"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students = pd.DataFrame({\"姓名\": name, \"性别\": gender, \"身高\": height})\n",
    "students"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0378a983",
   "metadata": {},
   "source": [
    "#### 打印`students`所有索引和列名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8071615a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['001', '002', '003', '004', '005', '006'], dtype='object')\n",
      "Index(['姓名', '性别', '身高'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "print(students.index)\n",
    "print(students.columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc260f05",
   "metadata": {},
   "source": [
    "#### 输出把`students`的行和列进行转置后的结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "64b971f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>001</th>\n",
       "      <th>002</th>\n",
       "      <th>003</th>\n",
       "      <th>004</th>\n",
       "      <th>005</th>\n",
       "      <th>006</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>姓名</th>\n",
       "      <td>小陈</td>\n",
       "      <td>小李</td>\n",
       "      <td>小王</td>\n",
       "      <td>小张</td>\n",
       "      <td>小赵</td>\n",
       "      <td>小周</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <td>男</td>\n",
       "      <td>女</td>\n",
       "      <td>男</td>\n",
       "      <td>男</td>\n",
       "      <td>女</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>身高</th>\n",
       "      <td>172.5</td>\n",
       "      <td>168.0</td>\n",
       "      <td>178.2</td>\n",
       "      <td>181.3</td>\n",
       "      <td>161.7</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      001    002    003    004    005  006\n",
       "姓名     小陈     小李     小王     小张     小赵   小周\n",
       "性别      男      女      男      男      女    女\n",
       "身高  172.5  168.0  178.2  181.3  161.7  NaN"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5627c4bb",
   "metadata": {},
   "source": [
    "#### 输出`students`的身高列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "20b7ba86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "001    172.5\n",
       "002    168.0\n",
       "003    178.2\n",
       "004    181.3\n",
       "005    161.7\n",
       "006      NaN\n",
       "Name: 身高, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students[\"身高\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a2fd432",
   "metadata": {},
   "source": [
    "#### 输出`students`的性别和身高列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "79722a5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>001</th>\n",
       "      <td>男</td>\n",
       "      <td>172.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002</th>\n",
       "      <td>女</td>\n",
       "      <td>168.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>003</th>\n",
       "      <td>男</td>\n",
       "      <td>178.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>004</th>\n",
       "      <td>男</td>\n",
       "      <td>181.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>005</th>\n",
       "      <td>女</td>\n",
       "      <td>161.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>006</th>\n",
       "      <td>女</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    性别     身高\n",
       "001  男  172.5\n",
       "002  女  168.0\n",
       "003  男  178.2\n",
       "004  男  181.3\n",
       "005  女  161.7\n",
       "006  女    NaN"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students[[\"性别\", \"身高\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72ad6e0a",
   "metadata": {},
   "source": [
    "#### 输出`students`索引为003的行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9c383c5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "姓名       小王\n",
       "性别        男\n",
       "身高    178.2\n",
       "Name: 003, dtype: object"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students.loc[\"003\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39ce3368",
   "metadata": {},
   "source": [
    "#### 输出`students`索引为003、004、005的行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8c07607b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>003</th>\n",
       "      <td>小王</td>\n",
       "      <td>男</td>\n",
       "      <td>178.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>004</th>\n",
       "      <td>小张</td>\n",
       "      <td>男</td>\n",
       "      <td>181.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>005</th>\n",
       "      <td>小赵</td>\n",
       "      <td>女</td>\n",
       "      <td>161.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     姓名 性别     身高\n",
       "003  小王  男  178.2\n",
       "004  小张  男  181.3\n",
       "005  小赵  女  161.7"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students.loc[\"003\":\"005\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "465ac365",
   "metadata": {},
   "source": [
    "#### 输出`students`里索引为005的身高。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "13b75e49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "161.7"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students.loc[\"005\", \"身高\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "853f2133",
   "metadata": {},
   "source": [
    "#### 输出`students`索引为003、005的数据的姓名和身高。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "70fc662c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>身高</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>003</th>\n",
       "      <td>小王</td>\n",
       "      <td>178.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>005</th>\n",
       "      <td>小赵</td>\n",
       "      <td>161.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     姓名     身高\n",
       "003  小王  178.2\n",
       "005  小赵  161.7"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students.loc[[\"003\", \"005\"], [\"姓名\", \"身高\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "297019f9",
   "metadata": {},
   "source": [
    "#### 输出`students`索引为003、005的数据的所有变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "748dc450",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>003</th>\n",
       "      <td>小王</td>\n",
       "      <td>男</td>\n",
       "      <td>178.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>005</th>\n",
       "      <td>小赵</td>\n",
       "      <td>女</td>\n",
       "      <td>161.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     姓名 性别     身高\n",
       "003  小王  男  178.2\n",
       "005  小赵  女  161.7"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students.loc[[\"003\", \"005\"], :]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffdbd372",
   "metadata": {},
   "source": [
    "#### 输出`students`里身高大于165的女性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b43592f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>002</th>\n",
       "      <td>小李</td>\n",
       "      <td>女</td>\n",
       "      <td>168.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     姓名 性别     身高\n",
       "002  小李  女  168.0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students[(students[\"身高\"] > 165) & (students[\"性别\"] == \"女\")]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6947f5c6",
   "metadata": {},
   "source": [
    "#### 通过`head`方法，输出`students`前五行数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "3ccdd6ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>001</th>\n",
       "      <td>小陈</td>\n",
       "      <td>男</td>\n",
       "      <td>172.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002</th>\n",
       "      <td>小李</td>\n",
       "      <td>女</td>\n",
       "      <td>168.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>003</th>\n",
       "      <td>小王</td>\n",
       "      <td>男</td>\n",
       "      <td>178.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>004</th>\n",
       "      <td>小张</td>\n",
       "      <td>男</td>\n",
       "      <td>181.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>005</th>\n",
       "      <td>小赵</td>\n",
       "      <td>女</td>\n",
       "      <td>161.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     姓名 性别     身高\n",
       "001  小陈  男  172.5\n",
       "002  小李  女  168.0\n",
       "003  小王  男  178.2\n",
       "004  小张  男  181.3\n",
       "005  小赵  女  161.7"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "students.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37877093-b209-474d-8a30-70243f64f646",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
